<?php
declare (strict_types=1);

namespace app\backend\controller\v1;

use app\api\validate\AddressValidate;
use app\common\model\NavBottom as NavBottomModel ;
use think\exception\ValidateException;
use think\Request;

class NavBottom extends BaseController
{
    /**
     * navbttom
     *
     * @return \think\Response
     */
    public function index()
    {
        // 导航子表
        $model = new NavBottomModel();
        // where条件
        $where = [];
        $title = input('get.title/s');
        if (!empty($title)) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 6);
        $sort = ['id' => 'desc'];
        //获取列表
        $list = $model->where($where)->with('getNav')->page($page, $pageSize)->order($sort)->select();
        $allTotal = $model->where($where)->count();
        return sucessMsg(200, '获取成功！', ['list' => $list, 'total' => count($list), 'allTotal' => $allTotal, 'page' => $page]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if (request()->isPost()) {
            $requestData = input('post.');
            try {
                //  验证参数
                validate(AddressValidate::class)->scene('create')->check($requestData);
                //新增会员
                $user = new NavBottomModel();
                if ($user->save()) {
                    return sucessMsg(200, '创建成功！', $user->id);
                }
                   return errMsg(500, '创建失败！');
            } catch (ValidateException $e) {
                return errMsg(400, $e->getError());
            }
        }
        return errMsg(500, '请求错误！');
    }

}